package com.chenfan.mcn.component;

import com.alibaba.fastjson.JSON;
import com.chenfan.mcn.enums.CommentBusinessTypeEnum;
import com.chenfan.notify.MessageClient;
import com.chenfan.notify.vo.MessageDetailVo;
import com.chenfan.notify.vo.MessagePlanVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author: WQ
 * Date 2022/10/31
 * @描述
 */
@Slf4j
@Component
public class MessageSender {

    @Autowired
    private MessageClient messageClient;

    /**
     * 发送通知消息
     */
    public void sendNotify(CommentBusinessTypeEnum commentEnum, Long tenantId, String template, List<Long> targets, Map<String, String> param) {
        MessagePlanVo planVo = new MessagePlanVo();
        planVo.setTenantId(ObjectUtils.defaultIfNull(tenantId, 1L));
        planVo.setMessageKey(UUID.randomUUID().toString());
        planVo.setJumpUrlType(commentEnum.getJumpUrlType());
        planVo.setTemplate(template);
        List<MessageDetailVo> details = new ArrayList<>();
        targets.forEach(target -> {
            MessageDetailVo messageDetailVo = new MessageDetailVo();
            messageDetailVo.setUserId(target);
            param.put("url", commentEnum.getUrl());
            messageDetailVo.setExternParams(param);
            details.add(messageDetailVo);
        });
        planVo.setDetails(details);
        log.info("sendNotify : {}" , JSON.toJSON(planVo));
        messageClient.sendMessage(planVo);
    }
}
